from typing import Optional


class FavoriteQuestion:
    """收藏题目实体类"""
    def __init__(self,
                 id: Optional[int] = None,
                 question_id: Optional[int] = None,
                 category: Optional[str] = None,
                 user_id: Optional[int] = None):
        """
        初始化收藏题目实体
        :param id: 收藏ID
        :param question_id: 题目ID
        :param category: 题目分类
        :param user_id: 用户ID
        """
        self.id = id
        self.question_id = question_id
        self.category = category
        self.user_id = user_id

    def __dict__(self) -> dict:
        """将实体转换为字典"""
        return {
            'id': self.id,
            'question_id': self.question_id,
            'category': self.category,
            'user_id': self.user_id
        }

    @classmethod
    def from_dict(cls, data: dict) -> 'FavoriteQuestion':
        """从字典创建实体"""
        return cls(
            id=data.get('id'),
            question_id=data.get('question_id'),
            category=data.get('category'),
            user_id=data.get('user_id')
        )